package other;

import main.Resource;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 工具类
 *
 * @author fdh
 */
public class Tool {
    private static Tool instance;

    public Tool() {
        if (instance == null)
            instance = this;
    }

    public static Tool getInstance() {
        return instance;
    }

    /**
     * 判断指定字符串是否为字母或数字或下划线组成
     *
     * @param str 指定字符串
     * @return
     */
    public boolean isLetterOrDigitOrUnderLine(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isLetterOrDigit(str.charAt(i)) && str.charAt(i) != '_') {
                return false;
            }
        }
        return true;
    }

    public JLabel getSongTiJLabel(String txt, int fontSize) {
        JLabel label = new JLabel(txt);
        label.setFont(new Font(Resource.LoginFrameResource.songTiTxtFontFamily, Font.BOLD, fontSize));
        return label;
    }

    public JLabel getMSYaHeiJLabel(String txt, int fontSize) {
        JLabel label = new JLabel(txt);
        label.setFont(new Font(Resource.LoginFrameResource.msYaHeiTxtFontFontFamily, Font.BOLD, fontSize));
        return label;
    }

    public String[] mySplit(String line, char splitChar) {
        StringBuilder a = new StringBuilder();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < line.length(); i++) {
            if (line.charAt(i) == splitChar) {
                list.add(a.toString());
                a = new StringBuilder();
            } else {
                a.append(line.charAt(i));
            }
        }
        list.add(a.toString());
        return list.toArray(new String[0]);
    }

    public String[][] setArrayEmpty(String[][] d) {
        for (int i = 0; i < d.length; i++) {
            Arrays.fill(d[i], "");
        }
        return d;
    }

    public String[] setArrayEmpty(String[] d) {
        Arrays.fill(d, "");
        return d;
    }
}
